package com.bawei.day12_broadcastrecevier;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.telecom.TelecomManager;
import android.telephony.TelephonyManager;
import android.util.Log;

/***
 系统广播：
 1。咱们之前写的自定义广播：发送方+接收方
 2。系统广播：只写接收方
 */
public class Main5Activity extends AppCompatActivity {
    private MySystemRecevicer mySystemRecevicer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},101);
        }
        mySystemRecevicer = new MySystemRecevicer();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        intentFilter.addAction(Intent.ACTION_USER_PRESENT);
        intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);
        intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intentFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
        registerReceiver(mySystemRecevicer,intentFilter);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mySystemRecevicer);
    }
    class MySystemRecevicer extends BroadcastReceiver{
        private static final String TAG ="ytx" ;
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(Intent.ACTION_SCREEN_ON.equals(action)){
                Log.i(TAG, "onReceive: 亮了");
            }else if(Intent.ACTION_SCREEN_OFF.equals(action)){
                Log.i(TAG, "onReceive: 暗了");
            }else if (Intent.ACTION_USER_PRESENT.equals(action)){
                Log.i(TAG, "onReceive: 唤醒了");
            }else if(Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)){
                Log.i(TAG, "onReceive: 飞行了");
            }else if(TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)){
                Log.i(TAG, "onReceive: 电话");
            }else if(Intent.ACTION_HEADSET_PLUG.equals(action)){
                Log.i(TAG, "onReceive: 耳机");
            }
        }
    }



}
